package com.fadadianqi.logistics.customer.service;

import com.fadadianqi.commons.PrimaryKeyCreator;
import com.fadadianqi.logistics.customer.dao.MsgJobDAO;
import com.fadadianqi.logistics.customer.dao.MsgQueueDAO;
import com.fadadianqi.logistics.customer.model.Customer;
import com.fadadianqi.logistics.customer.model.MsgJob;
import com.fadadianqi.logistics.customer.model.MsgQueue;
import com.yeqiangwei.commons.util.Pagination;
import com.yeqiangwei.commons.util.Utils;
import com.yeqiangwei.commons.util.ValidatorHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service("msgSendJob")
public class MsgSendJob {

    public int countAllJobs() {
        return msgJobDAO.countAll();
    }

    public void updateJob(MsgJob job) {
        msgJobDAO.update(job);
    }

    @Transactional
    public void delJob(String jobId) {
        this.cancelQueueByJobId(jobId);
        msgQueueDAO.removeByJobId(jobId);
        msgJobDAO.remove(getMsgJobById(jobId));
    }

    public List<MsgJob> findAllJobs(Pagination pagination) {
        pagination.setOrderList(new Pagination.Order[]{
                Pagination.getNewOrder("add_time", Pagination.Order.DESCENDING)
        });
        return msgJobDAO.findAll(pagination);
    }

    public List<MsgQueue> findAllQueue(String jobId, int status, Pagination pagination) {
        pagination.setOrderList(new Pagination.Order[]{
                Pagination.getNewOrder("add_time", Pagination.Order.DESCENDING)
        });
        return msgQueueDAO.findByJobIdAndStatus(jobId, status, pagination);
    }

    public List<MsgQueue> findAllQueue(String jobId, Pagination pagination) {
        pagination.setOrderList(new Pagination.Order[]{
                Pagination.getNewOrder("add_time", Pagination.Order.DESCENDING)
        });
        return msgQueueDAO.findByJobId(jobId, pagination);
    }

    public void updateQueue(MsgQueue queue) {
        msgQueueDAO.update(queue);
    }

    public int countQueueByJobId(String id) {
        return msgQueueDAO.countByJobId(id);
    }

    public int countQueueByJobIdAndStatus(String jobId, int status) {
        return msgQueueDAO.countQueueByJobIdAndStatus(jobId, status);
    }

    private int saveToQueue(MsgJob job, List<Customer> list) {
        int i = 0;
        for (Customer c : list) {
            String s = c.getMobile();
            if (ValidatorHelper.isEmpty(s)) {
                continue;
            }
            String[] m = s.split(" ");
            for (String mobile : m) {
                if (!Utils.isMobileNumber(mobile)) {
                    continue;
                }
                MsgQueue mq = new MsgQueue();
                mq.setId(PrimaryKeyCreator.create());
                mq.setAddTime(new Date());
                mq.setUpdateTime(new Date());
                mq.setContent(job.getContent());
                mq.setCustomerId(c.getId());
                mq.setMobile(mobile);
                mq.setMsgJobId(job.getId());
                mq.setStatus(MsgQueue.STATUS_ALREADY);
                msgQueueDAO.create(mq);
                i++;
            }
        }
        return i;
    }

    /**
     * 创建短信发送任务
     * 向某个城镇发送短信
     *
     * @param townId
     * @param content
     */
    @Transactional
    public void sendToTown(String[] townIds, MsgJob msgJob) {
        if (ValidatorHelper.isEmpty(townIds)) {
            return;
        }
        int c = 0;

        msgJob.setId(PrimaryKeyCreator.create());
        msgJob.setStatus(MsgJob.STATUS_PAUSE);
        msgJob.setAddTime(new Date());

        for (String townId : townIds) {
            int count = customerService.countByTownId(townId);
            int max = count;
            if (max > 2000) {
                max = 2000;
            }
            int pages = count / max;
            if (count % max != 0) {
                pages++;
            }

            for (int i = 1; i <= pages; i++) {
                Pagination pagination = new Pagination(i, max);
                List<Customer> list = customerService.findAllByTownId(townId, pagination);
                c = c + saveToQueue(msgJob, list);
            }
        }
        msgJob.setTotal(c);
        msgJobDAO.create(msgJob);
    }

    public MsgJob getMsgJobById(String id) {
        return msgJobDAO.getById(id);
    }

    public MsgQueue getMsgQueueById(String queueId) {
        return msgQueueDAO.getById(queueId);
    }

    public void cancelQueueByJobId(String jobId) {
        msgQueueDAO.cancelQueueByJobId(jobId);
    }

    @Autowired
    private MsgJobDAO msgJobDAO;

    @Autowired
    private MsgQueueDAO msgQueueDAO;

    @Autowired
    private CustomerService customerService;
}
